package cn.ztq.test0614.blue;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.ztq.test0614.R;
import cn.ztq.test0614.receiver.BluetoothReceiver;
import cn.ztq.test0614.receiver.ScanListener;
import cn.ztq.test0614.ui.home.RVDeviceAdapter;


public class BLEActivity extends AppCompatActivity implements View.OnClickListener, ScanListener {
    private RVDeviceAdapter rvDeviceAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        initView();
        initListener();
    }

    private void initListener() {
    }

    private void initView() {
        Button button = findViewById(R.id.button);
        Button button2 = findViewById(R.id.button2);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        rvDeviceAdapter = new RVDeviceAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(rvDeviceAdapter);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button) {
            scanBluetooth();
        } else if (v.getId() == R.id.button2) {
            BluetoothLeScanner bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            bluetoothLeScanner.startScan(new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    super.onScanResult(callbackType, result);


                    if (ActivityCompat.checkSelfPermission(BLEActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                        Log.e("", "没有连接------------------------");
                        return;
                    }
                    if (result == null) return;
                    if (result.getDevice().getName() != null)
                        Log.e("==", "name=" + result.getDevice().getName());
                    if (result.getScanRecord() != null) {
                        SparseArray<byte[]> manufacturerSpecificData = result.getScanRecord().getManufacturerSpecificData();
//                            Log.e("==","===="+manufacturerSpecificData);
                    }


                    BluetoothDevice device = result.getDevice();
                    BluetoothClass bClass = device.getBluetoothClass();
                    if (bClass.getMajorDeviceClass() == BluetoothClass.Device.Major.AUDIO_VIDEO && bClass.getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                        Log.e("BluetoothDevice", "name=" + device.getName());
                        Log.e("BluetoothDevice", "type=" + device.getType());
                        Log.e("BluetoothDevice", "address=" + device.getAddress());
                        Log.e("BluetoothDevice", "boundState=" + device.getBondState());
                        Log.e("BluetoothDevice", "uuids=" + Arrays.toString(device.getUuids()));

                        Log.e("s", "-------");
                        Log.e("bluetoothClass", "deviceClass=" + bClass.getDeviceClass());
                        Log.e("bluetoothClass", "majorDeviceClass=" + bClass.getMajorDeviceClass());
                        Log.e("bluetoothClass", "majorDeviceClass=" + bClass.describeContents());

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                            Log.e("BluetoothDevice", "alias=" + device.getAlias());
                        }
                        Log.e("111", "|");
                        Log.e("111", "|");
                        Log.e("111", "|");
                        Log.e("111", "|");

                    }
//                    Log.e("在扫描", "在扫描------------------------");

                }

                @Override
                public void onBatchScanResults(List<ScanResult> results) {
                    super.onBatchScanResults(results);
                    Log.e("扫描", "扫描：：：：：：：：：+onBatchScanResults");
                }

                @Override
                public void onScanFailed(int errorCode) {
                    super.onScanFailed(errorCode);
                    Log.e("扫描", "扫描：：：：：：：：：+onScanFailed");
                }
            });

        }

    }

    private void scanBluetooth() {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        defaultAdapter.startDiscovery();
        IntentFilter iFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        iFilter.addAction(BluetoothDevice.ACTION_FOUND);
        iFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(new BluetoothReceiver(this), iFilter);
    }

    @Override
    public void iScan(Map<String, BluetoothDevice> map) {
        List<BluetoothDevice> collect = new ArrayList<>(map.values());
        rvDeviceAdapter.setList(collect);
    }
}
